package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: SGS
 * @Date : 2020/11/16 22:11
 * @Version :
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据分类查询规格组
     * @param id
     * @return
     */
     @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long id){
         List<SpecGroup> list = groupService.query().eq("category_id", id).list();
         return ResponseEntity.ok(SpecGroupDTO.convertEntityList(list));
     }

    /**
     * 查询规格参数集合
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 过滤信息
     * @return
     */
     @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam(value = "categoryId",required = false) Long categoryId,
                                                              @RequestParam(value = "groupId",required = false) Long groupId,
                                                              @RequestParam(value = "searching",required = false) Boolean searching){

         return ResponseEntity.ok(paramService.queryParams(categoryId,groupId,searching));
     }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
     @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
         groupService.save(specGroupDTO.toEntity(SpecGroup.class));
         return ResponseEntity.status(HttpStatus.CREATED).build();
     }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
     @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
         groupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
         return ResponseEntity.status(HttpStatus.CREATED).build();
     }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
     @PostMapping("param")
    public ResponseEntity<Void> querySpecParams(@RequestBody SpecParamDTO specParamDTO){
         paramService.save(specParamDTO.toEntity(SpecParam.class));
         return ResponseEntity.status(HttpStatus.CREATED).build();
     }

    /**
     * 更新规格参数
     * @param specParamDTO
     * @return
     */
      @PutMapping("param")
    public  ResponseEntity<Void> updateSpecParams(@RequestBody SpecParamDTO specParamDTO){
         paramService.updateById(specParamDTO.toEntity(SpecParam.class));
         return ResponseEntity.status(HttpStatus.CREATED).build();
      }

    /**
     * 根据分类id查询规格组及组内参数
     * @param id
     * @return
     */
    @GetMapping("/list")
      public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long id){
          return ResponseEntity.ok(groupService.querySpecList(id));
    }
}


